package com.xs.designs.strategy.factory.template_method.IIII;

import com.xs.designs.strategy.factory.template_method.II.AliPay;
import com.xs.designs.strategy.factory.template_method.II.UnionPay;
import com.xs.designs.strategy.factory.template_method.II.WeChatPay;
import com.xs.designs.strategy.factory.template_method.IIIII.AbstractPaymentStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class PaymentStrategyFactory implements ApplicationContextAware {
    ApplicationContext applicationContext;

    public AbstractPaymentStrategy getPaymentStrategy(int payType) {
        switch (payType) {
            case 1:
                return applicationContext.getBean(WeChatPay.class);
            case 2:
                return applicationContext.getBean(AliPay.class);
            case 3:
                return applicationContext.getBean(UnionPay.class);
            default:
                throw new IllegalArgumentException("无效的支付类型");
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}